export const formDrag = {
   mounted:function (el, binding) {
    const dialogHeaderEl = el.querySelector('.el-dialog__header')
    const dragDom = el.querySelector('.el-dialog')
    dialogHeaderEl.style.cursor = 'move'
    const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null)

    dialogHeaderEl.onmousedown = (e) => {
      const screenWidth = document.querySelector('.menu_content').clientWidth
      const screenHeight = document.documentElement.clientHeight
      const dragDomWidth = dragDom.offsetWidth
      const dragDomheight = dragDom.offsetHeight
      const minDragDomLeft = dragDom.offsetLeft
      const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth
      const minDragDomTop = dragDom.offsetTop
      const maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomheight

      const disX = e.clientX - dialogHeaderEl.offsetLeft
      const disY = e.clientY - dialogHeaderEl.offsetTop

      let styL, styT

      if (sty.left.includes('%')) {
        styL = +document.querySelector('.common-wrap').clientWidth * (+sty.left.replace(/\%/g, '') / 100)
        styT = +document.querySelector('.common-wrap').clientHeight * (+sty.top.replace(/\%/g, '') / 100)
      } else {
        styL = +sty.left.replace(/\px/g, '')
        styT = +sty.top.replace(/\px/g, '')
      }

      document.onmousemove = function(e) {
        let l = e.clientX - disX
        let t = e.clientY - disY
        
        if (-l > minDragDomLeft) {
          l = -minDragDomLeft
        } else if (l > maxDragDomLeft) {
          l = maxDragDomLeft
        }
        if (-t > minDragDomTop) {
          t = -minDragDomTop
        } else if (t > maxDragDomTop + document.querySelector('.el-dialog__body').clientHeight - 60) {
          t = maxDragDomTop + document.querySelector('.el-dialog__body').clientHeight - 60
        }
        
        dragDom.style.left = `${l + styL}px`
        dragDom.style.top = `${t + styT}px`
      }

      document.onmouseup = function(e) {
        document.onmousemove = null
        document.onmouseup = null
      }
    }
  }
}